<template>
  <div
    class="dashboard-container"
    v-loading='loading'
  >
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <treetools
          :treeNode="company"
          :isRoot='true'
          @addDepts="addDepts"
        > </treetools>
        <el-tree
          :data="departs"
          :props="defaultProps"
        >
          <treetools
            slot-scope="{data}"
            :treeNode='data'
            @delDepts="getdepartment"
            @addDepts="addDepts"
            @editDepts='editDepts'
          ></treetools>
        </el-tree>
      </el-card>
    </div>
    <adddepts
      :showDialog.sync='showDialog'
      :treeNode="node"
      @addDepts="getdepartment"
      ref="addDept"
    ></adddepts>
  </div>

</template>

<script>
import treetools from './component/tree-tools.vue'
import adddepts from './component/add-dept.vue'

import { getdepartment } from '@/api/department'
import { tranListToTreeData } from '@/utils'
export default {
  components: {
    treetools,
    adddepts
  },
  data () {
    return {
      loading: false,
      node: null,
      showDialog: false, // 显示窗体
      company: {

      },
      defaultProps: {
        label: 'name'
      },
      departs: []
    }
  },
  created () {
    this.getdepartment()
  },
  methods: {
    async getdepartment () {
      this.loading = true
      const result = await getdepartment()
      // console.log(result);
      this.company = { name: result.companyName, manager: '负责人 ', id: '' }
      this.departs = tranListToTreeData(result.depts, '')
      this.loading = false
    },
    addDepts (node) {
      this.showDialog = true // 显示弹层
      // 因为node是当前的点击的部门， 此时这个部门应该记录下来,
      this.node = node
    },
    editDepts (node) {
      this.showDialog = true
      this.node = node
      //在这里调用获取部门详情的方法
      this.$refs.addDept.getdepartdetail(node.id)
    },


  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
